layui.use(['treeTable', 'jquery', 'laydate', 'febs', 'form', 'commonJS', 'eleTree', 'validate'], function () {
    var $ = layui.jquery,
        layer = layui.layer,
        febs = layui.febs,
        table = layui.table,
        treeTable = layui.treeTable,
        form = layui.form,
        validate = layui.validate,
        $view = $('#febs-deptNew'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#deptNew-table-form'),
        $tenantSelect = $searchForm.find('select[name="tenantSelect"]'),
    tableIns;

    form.verify(validate);
    form.render();
    tableIns = treeTable.render({
        elem:'#deptNewTable',
        url:ctx + 'dept/treeNew',
        height:'full-200',
        id:'deptNew',
        tree:{
          view:{
              showIcon:false,
              dblClickExpand:true,
              expandAllDefault:true
          }
        },
        page:true,
        search:true,
        request: {
            pageName: 'pageNum',
            limitName: 'pageSize'
        },
        cols:[[
            {field: 'name', title: '机构名称', minWidth: 220},
            {field: 'code', title: '机构编号', width: 200},
            {field: 'type', title: '机构类型', align:'center', width: 100,templet:function (d) {
                    let typeStr = '';
                    if(d.type == '1'){
                        typeStr = '<span class="layui-badge febs-tag-cyan">本部</span>';
                    }else if(d.type == '2'){
                        typeStr = '<span class="layui-badge febs-tag-blue">部门</span>';
                    }else if(d.type == '3'){
                        typeStr = '<span class="layui-badge febs-tag-orange">科室</span>';
                    }else if(d.type == '4'){
                        typeStr = '<span class="layui-badge febs-tag-purple">职场</span>';
                    }else if(d.type == '5'){
                        typeStr = '<span class="layui-badge febs-tag-lime">班组</span>';
                    }
                    return typeStr;
                } },
            {field: 'businessCode',align: "center", title: '业务编码', width: 120},
            {field: 'orderNum',align: "center", title: '机构排序', width: 100},
            {field: 'status',align: "center", title: '状态',toolbar: '#status-bar', width: 120},
            {title: '操作', toolbar: '#deptNew-option',align:'center', width: 240}
        ]],
        toolbar: '#deptNew-toolbar',
        defaultToolbar:'',
        done:function (data) {
            console.log(data);
            layer.closeAll('loading');
        },
        error :function (error,data) {
            console.log(error);
        }

    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    table.on('toolbar(deptNewTable)', function (obj) {
        if (obj.event === 'add') {
            febs.modal.open('新增菜单', 'system/dept/addDept', {
                btn: ['提交', '取消'],
                area: [$(window).width() <= 750 ? '90%' : '800px','600px'],
                yes: function (index, layero) {
                    $('#dept-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data:{
                    tenantId: $tenantSelect.val(),
                }
            });
        }
    });

    table.on('tool(deptNewTable)', function (obj) {
        var data = obj.data.data;
        if (obj.event === 'del') {
            febs.modal.confirm('删除数据', '确定删除所选数据？', function () {
                deleteIds(data.deptId);
            });
        }
        if (obj.event === 'add') {
            febs.modal.open('新增菜单', 'system/dept/addDept', {
                btn: ['提交', '取消'],
                area: [$(window).width() <= 750 ? '90%' : '800px','600px'],
                yes: function (index, layero) {
                    $('#dept-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data:{
                    parentId:data.deptId,
                    parentName:data.deptName,
                    tenantId: $tenantSelect.val(),
                }
            });
        }
        if (obj.event === 'edit') {
            febs.modal.open('新增菜单', 'system/dept/updateDept/'+data.deptId, {
                btn: ['提交', '取消'],
                area: [$(window).width() <= 750 ? '90%' : '800px','600px'],
                yes: function (index, layero) {
                    $('#dept-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data:{
                    tenantId: $tenantSelect.val(),
                }
            });
        }
    });

    function deleteIds(ids) {
        febs.get(ctx +'dept/delete/' + ids, null, function () {
            febs.alert.success('删除数据成功');
            $query.trigger('click');
        })
    }

    form.on('switch(status)', function (obj) {
        //区域状态控制点击事件
        var checked = obj.elem.checked;
        var status;
        if (checked) {
            status = "1";
        } else {
            status = "0";
        }
        //获取该控件所在行
        var index = obj.othis.parents('tr').eq(0).data('index');
        //获取表中所有数据 按行取数据及其 postId
        var id =  table.cache['deptNew'][index].id;
        febs.modal.confirm('更改状态', '确认更改状态',function (){
            updateStatus(id,status);
        },function (){
            obj.elem.checked = !checked;
            layer.closeAll();
            form.render('checkbox');
        });

    });
    function updateStatus(id,status) {
        //更新启用状态
        let model={
            deptId : id,
            status : status
        }
        febs.postArray(ctx + 'dept/updateStatus', model, function () {
            febs.alert.success('修改状态成功');
        })
    }

    function getQueryParams() {
        return {
            deptName: $searchForm.find('input[name="deptName"]').val()?.trim(),
            tenantId: $tenantSelect.val(),
            invalidate_ie_cache: new Date()
        };
    }

});